<?php
	require_once "include/User.php";
	require_once "include/Article.php";
	require_once "include/Template.php";
	require_once "include/FormTempl.php";
	require_once "include/backend/UserDb.php";
	require_once "include/backend/ArticlesDb.php";
	
	session_start();
	if (!isset($_SESSION['user']->name)) // refuse access when no one logged in
		header("Location: ./");
	
	if ($_GET['act'] == "write") {
		$page = new Template();
		$page->setUser($_SESSION['user']);
		
		if      ($_GET['err'] == "empty")
			$page->addContentFromFile("include/html/status/emptyfields.html");
		else if ($_GET['err'] == "notimage")
			$page->addContentFromFile("include/html/status/notanimage.html");
		
		$form = new FormTempl("article");
		$page->addContent($form->getHTML());
		
		echo $page->getHTML();
	}
	else if ($_GET['act'] == "edit" && is_numeric($_GET['id'])) {
		$articles = new ArticlesDb();
		$article = $articles->get(1, $_GET['id']);
		$article = $articles->result2array($article);
		
		$page = new Template();
		$page->setUser($_SESSION['user']);
		
		$form = new FormTempl("article", new Article($article[0]));
		$page->addContent($form->getHTML());
		
		echo $page->getHTML();
	}
	else
		header("Location: ./");
?>
